﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using PluginInterface;
namespace QUANLYDIEMHOCSINH
{
    public partial class ChinhAnhNen : Form
    {
        public ChinhAnhNen()
        {
            InitializeComponent();
            LoadPlugins();
            DocFile();
        }
        void DocFile()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                while ((s = sr.ReadLine()) != null)
                {
                    cbnen.SelectedItem = s;

                }
                this.BackColor = Color.FromName(cbnen.SelectedItem.ToString());
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    cbnen.SelectedItem = s;

                }
                this.BackColor = Color.FromName(cbnen.SelectedItem.ToString());
                fs1.Close();
            }
        }
        void GhiFile()
        {
            FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(cbnen.SelectedItem.ToString());
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        List<IPplug> plugins = new List<IPplug>();
        private void LoadPlugins()
        {

            // Load others  cho naydebug t xem 
            IPplug plugX = null;
            string[] files = Directory.GetFiles(
                Application.StartupPath + @"\Addon",
                "*.ext");

            foreach (string f in files)
            {
                Assembly asm = Assembly.LoadFile(f);
                Type[] types = asm.GetTypes();
                string className =
                    Path.GetFileNameWithoutExtension(f) +
                    ".PlugClass";


                foreach (Type t in types)
                {
                    if (t.FullName == className)
                    {
                        plugX = (IPplug)Activator.CreateInstance(t);
                        plugins.Add(plugX);
                    }
                }
            }

            // Load the plugin list to combobox
            foreach (IPplug p in plugins)
            {
                string green = p.Get_green();
                cbnen.Items.Add(green);
                string gray = p.Get_gray();
                cbnen.Items.Add(gray);
                string blue = p.Get_blue();
                cbnen.Items.Add(blue);

            }
        }

        private void btnen_Click(object sender, EventArgs e)
        {
            GhiFile();
            this.BackColor = Color.FromName(cbnen.SelectedItem.ToString());

        }

        private void btnen_Click_1(object sender, EventArgs e)
        {
            GhiFile();
            this.BackColor = Color.FromName(cbnen.SelectedItem.ToString());
        }
      
    }
}
